<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\FeishuWebAuth;
use App\Http\Validate\Common\CommonValidate;
use App\Models\Common\ConfigModel;
use App\Service\Common\CommonService;
use App\Service\OpenShop\Feishu\ConfigService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ConfigController extends BaseController
{

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function config(Request $request): JsonResponse
    {
        $module = $request->get("module", "");
        $name   = $request->get("name", "");

        return $this->success(ConfigService::config($module, $name));
    }

    public function save(Request $request): JsonResponse
    {
        $module = $request->get("module", "");
        $name   = $request->get("name", "");
        $value  = $request->get('value', '');

        $this->validate($request, [
            'module' => 'required|string',
            'name'   => 'required|string',
            'value'  => 'required|string',
        ]);

        return $this->success(ConfigService::save($module, $name, $value, FeishuWebAuth::$userId));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getConfigContent(Request $request): JsonResponse
    {
        $params = CommonValidate::getConfigContentValidate($request->all());
        return $this->success(ConfigService::getConfigDetail($params->module, $params->name));
    }

    /**
     * 获得行政区域
     * @return JsonResponse
     * @throws Exception
     */
    public function getAmapDistrict(): JsonResponse
    {
        return $this->success(ConfigService::getAmapDistrictJson());
    }


    /**
     * 夸父选址省市区和code匹配规范
     * @return JsonResponse
     */
    public function regionMatchSpecs(): JsonResponse
    {
        return $this->success(ConfigService::regionMatchSpecs());
    }


    /**
     * 根据code获得天气情况
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function getWeatherByAdCode(Request $request)
    {
        $this->validate($request, [
            'adcode' => 'required'
        ]);
        return $this->success(CommonService::getWeatherByAdCode($request->adcode));
    }

    /**
     * 公共的配置查询
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function publicConfig(Request $request)
    {
        $this->validate($request, [
            'module'    => 'required|string',
            'name'      => 'required|string',
            'cacheTime' => 'integer',
        ]);

        $module    = $request->get("module", "");
        $name      = $request->get("name", "");
        $cacheTime = $request->get("cacheTime", 60);

        return $this->success(ConfigService::publicConfig($module, $name, $cacheTime));
    }
}
